<?php

namespace App\Models\OperationManagement;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Grimzy\LaravelMysqlSpatial\Types\Polygon;
use Grimzy\LaravelMysqlSpatial\Eloquent\SpatialTrait;
use App\Models\Constant\Fields;

class OtherAreas extends Model
{
    use SoftDeletes;
    use SpatialTrait;
    protected $guarded = [];
    protected $spatialFields = ['areas'];

    //查看列表--分页
    public function getList($request)
    {
        $per_page = $request->input('per_page',10);
        return self::orderBy('id','desc')
                ->where(function($query) use ($request){
                    //检测当前的请求中是否包含name、areas参数
                    $name = $request->input('name', '');
                    $level = $request->input('level','');
                    $areas = $request->input('areas','');
                    if(!empty($name)) {
                        $query->where('name','like','%'.$name.'%');
                    }
                    if(!empty($level)) {
                        $query->where('level',$level);
                    }
                    if(!empty($areas)){
                        $areas = Polygon::fromString($areas);
                        $query->distance('areas',$areas,10);
                    }
                })
                ->paginate($per_page);
    }
}
